package cn.skyquant.quant4j.jforex.sdk.strategy;

import com.dukascopy.api.Instrument;
import com.dukascopy.api.instrument.IInstrumentGroup;

import java.math.BigDecimal;
import java.math.RoundingMode;

import static com.dukascopy.api.Instrument.XAGUSD;
import static com.dukascopy.api.Instrument.XAUUSD;

public class InstrumentEntity {
    private final static BigDecimal b1 = new BigDecimal(1);
    public final double pipValue;
    public final double minAmount;//最小开仓单位
    public final double minMoney;//最小开仓单位1个点(不是JF里的点，而是屌丝网站里的点)能赚的钱
    public final int amountScale;
    public final int scale;
    public final int pipMulti;
    public final Instrument instrument;
    public final double minCharge;
    private final String formatPrice;
    private final String formatAmout;

    public InstrumentEntity(Instrument instrument) {
        this.instrument = instrument;
        pipValue = getPipValue(instrument);
        minAmount = getMinAmount(instrument);
        minMoney = getMinAmountToMoney(instrument);
        scale = instrument.getPipScale();
        pipMulti = b1.divide(new BigDecimal(pipValue), 0, RoundingMode.HALF_UP).intValue();
        amountScale = getAmountScale(instrument);
        minCharge = getMinCharge(instrument);
        formatPrice = "%." + (scale+1) + "f";
        formatAmout = "%." + (amountScale) + "f";
    }

    public String formatPrice(double v) {
        return String.format(formatPrice, v);
    }
    public String formatAmout(double v) {
        return String.format(formatAmout, v);
    }

    public static double getPipValue(Instrument instrument) {
        if (instrument == XAUUSD) {
            return 0.1;
        }
        return instrument.getPipValue();
    }

    //20个点,0.002 * 10000 * 0.1 * 0.001/0.001 =
    public double getMoney(double price, double amount) {
        return price * pipMulti * minMoney * (amount/minAmount);
    }

    public int priceToPip(double price){
        return new BigDecimal(price * pipMulti).setScale(0,RoundingMode.HALF_UP).intValue();
    }

    //获取手续费
    public double getCharge(double amount) {
        return minCharge*amount/minAmount;
    }

    //获取最小开仓单位
    public static double getMinAmount(Instrument instrument) {
        return (int)(instrument.getMinTradeAmount())*0.000001;
    }

    //获取最小开仓单位1个点(不是JF里的点，而是屌丝网站里的点)能赚的钱
    public static double getMinAmountToMoney(Instrument instrument) {
        if (instrument == Instrument.BRENTCMDUSD || instrument == Instrument.LIGHTCMDUSD) {
            return 1;
        } else {
            return 0.1;
        }
    }

    //手续费
    public static double getMinCharge(Instrument instrument) {
        if (instrument == Instrument.BRENTCMDUSD || instrument == Instrument.LIGHTCMDUSD) {
            return 30*2*0.001;
        } else {
            return 45*2*0.001;
        }
    }

    //获取最小开仓单位1个点(不是JF里的点，而是屌丝网站里的点)能赚的钱
    public static int getAmountScale(Instrument instrument) {
        return 6;
    }

    @Override
    public String toString() {
        return "InstrumentEntity{" +
                "pipValue=" + pipValue +
                ", minAmount=" + minAmount +
                ", minMoney=" + minMoney +
                ", amountScale=" + amountScale +
                ", scale=" + scale +
                ", pipMulti=" + pipMulti +
                ", instrument=" + instrument +
                '}';
    }
}
